<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录与轮播图管理系统</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            /* background-color: #f5f7fa; */
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #333;
            transition: all 0.3s ease;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        /* 登录页面样式 */
        .login-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-box {
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            padding: 40px;
            width: 100%;
            max-width: 400px;
        }

        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #2c3e50;
            font-size: 24px;
            font-weight: 600;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border 0.3s;
        }

        .form-control:focus {
            border-color: #667eea;
            outline: none;
            box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
        }

        .btn {
            display: block;
            width: 100%;
            padding: 12px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .btn:hover {
            background: #2980b9;
        }



        /* 管理界面样式 */
        .admin-container {
            display: none;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 0;
            border-bottom: 1px solid #eee;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #2c3e50;
        }

        .logout-btn {
            background: #e74c3c;
            padding: 8px 15px;
            border-radius: 5px;
            color: white;
            cursor: pointer;
            border: none;
            font-size: 14px;
        }

        .logout-btn:hover {
            background: #c0392b;
        }

        /* 轮播图样式 */
        .carousel-container {
            position: relative;
            width: 100%;
            height: 400px;
            overflow: hidden;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }

        .carousel-slide {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            transition: opacity 1s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #34495e;
            color: white;
            font-size: 24px;
        }

        .carousel-slide.active {
            opacity: 1;
        }

        .carousel-controls {
            position: absolute;
            bottom: 20px;
            left: 0;
            right: 0;
            text-align: center;
        }

        .carousel-dot {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.5);
            margin: 0 5px;
            cursor: pointer;
        }

        .carousel-dot.active {
            background: white;
        }

        .carousel-prev,
        .carousel-next {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background: rgba(0, 0, 0, 0.5);
            color: white;
            border: none;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 18px;
        }

        .carousel-prev {
            left: 15px;
        }

        .carousel-next {
            right: 15px;
        }

        .loading {
            text-align: center;
            padding: 20px;
            font-size: 18px;
            color: #7f8c8d;
        }

        .data-info {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }

        .footer {
            text-align: center;
            margin-top: 20px;
            color: #718096;
            font-size: 14px;
        }

        .error-message {
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            display: none;
            background-color: #fed7d7;
            color: #c53030;
            border: 1px solid #feb2b2;
        }
    </style>
</head>

<body>
    <!-- 登录页面 -->
    <div class="login-container" id="loginPage">
        <div class="login-box">

            <h2 class="login-title">用户登录</h2>

            <div class="error-message" id="errorMessage"></div>

            <form id="loginForm">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" class="form-control" placeholder="请输入用户名" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" class="form-control" placeholder="请输入密码" required>
                </div>
                <button type="submit" class="btn">登录</button>
                <!-- <div class="error-message" id="errorMessage"></div> -->
            </form>
            <div class="footer">
                <p>©大数据呈现技术</p>
            </div>
        </div>

    </div>

    <!-- 管理界面 -->
    <div class="admin-container" id="adminPage">
        <div class="container">
            <div class="header">
                <h1>轮播图管理系统</h1>
                <button class="logout-btn" id="logoutBtn">退出登录</button>
            </div>

            <div class="carousel-container">
                <div class="loading" id="loadingMessage">正在加载轮播图数据...</div>
                <div class="carousel-slide" id="slide1"></div>
                <div class="carousel-slide" id="slide2"></div>
                <div class="carousel-slide" id="slide3"></div>

                <button class="carousel-prev" id="prevBtn">&#10094;</button>
                <button class="carousel-next" id="nextBtn">&#10095;</button>

                <div class="carousel-controls">
                    <span class="carousel-dot active" data-slide="0"></span>
                    <span class="carousel-dot" data-slide="1"></span>
                    <span class="carousel-dot" data-slide="2"></span>
                </div>
            </div>

            <div class="data-info">
                <h3>数据信息</h3>
                <p>当前登录用户: <span id="currentUser"></span></p>
                <p>Token: <span id="tokenInfo"></span></p>
                <p>轮播图数据状态: <span id="dataStatus">未加载</span></p>
            </div>
        </div>
    </div>
    <script>
        // 轮播图控制
        let currentSlide = 0;
        const slides = $('.carousel-slide');
        const dots = $('.carousel-dot');

        function showSlide(n) {
            slides.removeClass('active');
            dots.removeClass('active');

            currentSlide = (n + slides.length) % slides.length;

            slides.eq(currentSlide).addClass('active');
            dots.eq(currentSlide).addClass('active');
        }

        $('#prevBtn').on('click', function () {
            showSlide(currentSlide - 1);
        });

        $('#nextBtn').on('click', function () {
            showSlide(currentSlide + 1);
        });

        dots.on('click', function () {
            const slideIndex = parseInt($(this).data('slide'));
            showSlide(slideIndex);
        });

        // 自动轮播
        setInterval(function () {
            showSlide(currentSlide + 1);
        }, 5000);
        // 显示错误信息
        function showError(message) {
            $('#errorMessage').text(message).show();
            setTimeout(function () {
                $('#errorMessage').hide();
            }, 3000);
        }
    </script>

    <script>
        let token = localStorage.getItem("token");
        if (token) {
            $("#loginPage").hide();
            $("#adminPage").show();
        } else {
            $("#loginPage").show();
            $("#adminPage").hide();
        }
          $("#logoutBtn").on('click', function () {
                localStorage.removeItem("token");
                localStorage.removeItem("username");
                $("#loginPage").show();
                $("#adminPage").hide();
                $("#loginForm")[0].reset();///表单重置
            })
        $("#loginForm").on('submit', function (e) {//绑定的方法
            e.preventDefault();//阻止默认的交互行为，阻止页面的刷新
            // 登录页面：
            let username = $("#username").val().trim();
            let password = $("#password").val().trim();
            // 用户名和密码的验证====>正则表达式来完成

            // 调用api
            $.ajax({
                url: "http://39.103.160.49:8888/user/login",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify({
                    username: username,
                    password: password
                }),
                success: function (res) {
                    console.log(res);
                    // 保存他的对应的token信息，然后实现页面的跳转
                    localStorage.setItem("token", res.data.token);
                    // localStorage.token=res.data.token;
                    localStorage.setItem("username", username);
                    $("#loginPage").hide();
                    $("#adminPage").show();
                      let token = localStorage.getItem("token");
                      $("#tokenInfo").text(token.substring(0,20)+"....")
                    lunbo();

                },
                error: function (xhr, status, error) {
                    showError("网络错误")
                }
            })

            function lunbo() {
                // 用户登录的状态
                // let token = localStorage.token;
                let token = localStorage.getItem("token");
                if (!token) {
                    $('#dataStatus').text("未登录").css("color", "red");
                    return;
                }
                $.ajax({
                    url: "http://39.103.160.49:8888/carousel/findAll",
                    type: "GET",
                    headers: {
                        'Authorization': localStorage.getItem("token")//把用户登录的信息传递给后端进行交互
                    },
                    success: function (res) {
                        console.log(res.status);
                        if (res.status === 200) {
                            $("#dataStatus").text("已加载").css("color", "green");
                            for (let i = 0; i < res.data.length; i++) {
                                let a = $("#slide" + (i + 1)).append("<img src='" + res.data[i].url + "'/>")
                                console.log(a);
                            }

                        } else {
                            $("#dataStatus").text("数据加载异常").css("color", "red");

                        }

                    },
                    error: function () {
                        $('#dataStatus').text("网络异常").css("color", "red");
                    }
                })

            }

        })




    </script>
</body>

</html>